package agent.handler.bs;

import agent.container.HttpForwardClient;
import agent.container.TcpForwardClient;
import common.body.TcpBody;
import common.enums.ProtocolEnum;
import common.handler.AbstractBsHandler;
import common.packet.BsPacket;
import common.packet.TcpPacket;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;

/**
 * @author gitama
 * @version 1.0
 * @date 2022/12/05
 */
public class TcpReqHandler extends AbstractBsHandler<TcpBody> {

    @Override
    public Class<TcpBody> bodyClass() {
        return TcpBody.class;
    }

    @Override
    public void handler(BsPacket packet, TcpBody bsBody, ChannelContext channelContext) throws Exception {
        String bsId = bsBody.getBsId();
        TcpPacket tcpPacket = new TcpPacket(bsBody.getBytes());

        if (ProtocolEnum.HTTP.getCode().equals(bsBody.getProtocol())) {
            Tio.sendToBsId(HttpForwardClient.clientTioConfig, bsId, tcpPacket);
        } else {
            Tio.sendToBsId(TcpForwardClient.clientTioConfig, bsId, tcpPacket);
        }
    }

}
